home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / comm / bbs / cit_src_7H21.lha / expand.c < prev    next >
C/C++ Source or Header  |  1997-08-11  |  9KB  |  385 lines

  1. /*
  2. *                               expand.c
  3. *
  4. * Expands a msg file.  V.1.2
  5. */
  6. #include "ctdl.h"    /* header file  */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11. #include <ctype.h>
  12. #include <time.h>
  13. #include <proto/exec.h>
  14. #include <pragmas/dos_pragmas.h>
  15. #include "exec/memory.h"
  16. #include "exec/ports.h"
  17. #include "exec/exec.h"
  18. #include "dos/dos.h"
  19. extern struct DosLibrary *DOSBase ;
  20.  
  21. /*
  22. *                               history
  23. *
  24. * 85Nov16 HAW  Modified for MS-DOS.
  25. * 85Apr22 HAW  Move to MS-DOS.
  26. * 84Dec09 HAW  Now merely expands a file, rather than copying & expanding.
  27. * 84Jun22 HAW  Version 1.1 created -- handles any file expansion.
  28. * 84Jun19 HAW  Created.
  29. */
  30. /*
  31. *                               contents
  32. *
  33. *       copy_remainder()        Copies remainder of split msg into file
  34. *       crashout()              irrecoverable error
  35. *       first_part()            Remembers remainder of split msg
  36. *       getUtilNumber()         prompt user for a number, limited range
  37. *       getUtilString()         gets a string from the console
  38. *       main()                  Main controller for this program
  39. */
  40. BPTR    Amsgfl;
  41. extern FILE *upfd;
  42. char            split_buffer[10000];
  43. extern          CONFIG cfg;
  44.  
  45. char mode[6];
  46.  
  47. unsigned copy_remainder ( unsigned offset );
  48. void crashout ( char *str );
  49. int first_part ( void );
  50. int getUtilNumber ( char *prompt , unsigned bottom , unsigned top );
  51. void getUtilString ( char *prompt , char *buf , int lim );
  52. void Expand_InitMsgBase ( void );
  53. void SopenFile ( char *filename , BPTR *fd );
  54. long myseek ( BPTR fd , long rpos , int mode );
  55. BPTR myopen ( char *name , char *mode );
  56. void myclose ( BPTR fd );
  57. long myread ( void *buff , int size , int blocks , BPTR fp );
  58. long mywrite ( void *buff , int size , int blocks , BPTR fp );
  59.  
  60. int NEUtilGetch(void);
  61. int UtilGetch(void);
  62. int  mPrintf(char *format, ...) {return 0; }  /* stub to quiet the linker */
  63.  
  64. /*
  65. * copy_remainder()
  66. *
  67. * This copies the remainder of the split msg onto the "end" of the file.
  68. */
  69. unsigned copy_remainder(offset)
  70. unsigned offset;
  71.   {
  72.   unsigned char *beg;
  73.   int  count, i;
  74.   beg = split_buffer;
  75.   count = 0;
  76.   printf("Transfer %d sectors to cover split msg\n",
  77.   (offset) ? offset / MSG_SECT_SIZE + 1 : 0);
  78.   while (1)
  79.     {
  80.     if (offset < MSG_SECT_SIZE) break;
  81.     crypte(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 0);
  82.     if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  83.       {
  84.       printf("\ncopy_remainder:Error writing first set %d aborting\n",count);
  85.       exit(100);
  86.       };
  87.     count++;
  88.     offset -= MSG_SECT_SIZE;
  89.  
  90.     }
  91.   if (offset == 0) return (unsigned)count;
  92.   for (i = 0; beg[MSG_SECT_SIZE * count + i] != 255; i++) ;
  93.   for (i++; i < MSG_SECT_SIZE; i++)
  94.   beg[MSG_SECT_SIZE * count + i] = 0;
  95.   crypte(beg + MSG_SECT_SIZE * count, MSG_SECT_SIZE, 0);
  96.   if (mywrite(beg + (MSG_SECT_SIZE * count), MSG_SECT_SIZE, 1, Amsgfl) != 1)
  97.     {
  98.     printf("\ncopy_remainder:Error writing second set %d aborting\n",count);
  99.     exit(100);
  100.     };
  101.   return (unsigned ) (count + 1);
  102.  
  103.   }
  104. /*
  105. * crashout()
  106. *
  107. * This handles the irrecoverable error.
  108. */
  109. void crashout(str)
  110. char *str;
  111.   {
  112.   printf(str);
  113.   exit(100);
  114.  
  115.   }
  116. /*
  117. * first_part()
  118. *
  119. * This remembers the remainder of first msg.
  120. */
  121. int first_part()
  122.   {
  123.   int  offset, i;
  124.   DATA_BLOCK buf;
  125.   char done;
  126.   offset = 0;
  127.   done   = FALSE;
  128.   do
  129.     {
  130.     if (myread(buf, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  131.       {
  132.       printf("\nError on read first message\n");
  133.       exit(100);
  134.       };
  135.     crypte(buf, MSG_SECT_SIZE, 0);
  136.     i = 0;
  137.     while (buf[i] != 255 && i < MSG_SECT_SIZE)
  138.     split_buffer[offset++] = buf[i++];
  139.     done = !(i == MSG_SECT_SIZE);
  140.  
  141.     }
  142.   while (!done);
  143.   myseek(Amsgfl, 0, SEEK_SET);  /* rewind to begining */
  144.   return offset;
  145.  
  146.   }
  147. /*
  148. * getUtilNumber()
  149. *
  150. * This function prompts for a number in (bottom, top) range.
  151. */
  152. int getUtilNumber(prompt, bottom, top)
  153. char   *prompt;
  154. unsigned bottom;
  155. unsigned top;
  156.   {
  157.   unsigned try;
  158.   char numstring[NAMESIZE];
  159.   do
  160.     {
  161.     getUtilString(prompt, numstring, NAMESIZE);
  162.     try     = atoi(numstring);
  163.     if (try < bottom)  printf("Sorry, must be at least %d\n", bottom);
  164.     if (try > top   )  printf("Sorry, must be no more than %d\n", top);
  165.  
  166.     }
  167.   while ((try < bottom ||  try > top));
  168.   return (int) try;
  169.  
  170.   }
  171. /*
  172. * getUtilString()
  173. *
  174. * This will get a string from the user.
  175. */
  176. void getUtilString(prompt, buf, lim)
  177. char *prompt;
  178. char *buf;
  179. int  lim;       /* max # chars to read */
  180.   {
  181.   char c;
  182.   int  i;
  183.   if (strLen(prompt) > 0)
  184.     {
  185.     printf("\nEnter %s\n : ", prompt, lim);
  186.  
  187.     }
  188.   i   = 0;
  189.   while (
  190.   c = NEUtilGetch(),
  191.   c     != NEWLINE
  192.   && c  != '\r'
  193.   && i     <  lim
  194.   )
  195.     {
  196.     /* handle delete chars: */
  197.     if (c == BACKSPACE)
  198.       {
  199.       putchar(' ');
  200.       putchar(BACKSPACE);
  201.       if (i > 0) i--;
  202.       else
  203.         {
  204.         putchar(' ');
  205.         putchar(BELL);
  206.  
  207.         }
  208.  
  209.       }
  210.     else
  211.       {
  212.       putchar(c);
  213.       buf[i++] = c;
  214.  
  215.       }
  216.     if (i >= lim)
  217.       {
  218.       putchar(BELL);
  219.       putchar(BACKSPACE); i--;
  220.  
  221.       }
  222.  
  223.     }
  224.   buf[i]  = '\0';
  225.  
  226.   }
  227. /*
  228. * main()
  229. *
  230. * This is the main controller.
  231. */
  232. int  main(void);
  233. int main()
  234.   {
  235.   char     temp[MSG_SECT_SIZE];
  236.   char c;
  237.   int      i, offset,cnt;
  238.   unsigned oldsize, fudge;
  239.   long work;
  240.   strcpy(mode,"rb+");
  241.   printf("Citadel Message Data File Expander %s \n%s\n", VERSION_NAME, COPYRIGHT);
  242.   printf("Have you backed up the ctdlmsg.sys file yet (y/n)? ");
  243.   c = getchar();
  244.   while( getchar() != '\n');
  245.   if ( c != 'Y' && c != 'y' )
  246.     {
  247.     printf("Please do so before proceeding.  Failure to do so may\n");
  248.     printf("result in you loosing everything...\n");
  249.     exit(1);
  250.  
  251.     }
  252.   printf("\n\nOne moment, please...\n");
  253.   cfg.weAre = UTILITY;
  254.   if (readSysTab(TRUE, TRUE))
  255.     {
  256.     if (access(LOCKFILE, 0) != -1)
  257.       {
  258.       printf("Please do not run Expand using Outside Commands.\n");
  259.       writeSysTab();
  260.       exit(1);
  261.  
  262.       }
  263.     cfg.weAre = UTILITY;
  264.     Expand_InitMsgBase();
  265.     oldsize     = cfg.maxMSector;
  266.     printf("\nOld size was %dK", cfg.maxMSector / (1024 / MSG_SECT_SIZE));
  267.     cfg.maxMSector = getUtilNumber("\nNew size (in decimal!)",
  268.     (unsigned) cfg.maxMSector/(1024/MSG_SECT_SIZE), 65535);
  269.     cfg.maxMSector *= (1024 / MSG_SECT_SIZE);
  270.     printf("\nThank you.  Working...");
  271.     offset = first_part();
  272.     writeSysTab();      /* Restore again, don't have to reconfigure */
  273.     work = oldsize;
  274.     work *= MSG_SECT_SIZE;
  275.     myseek(Amsgfl, 0,SEEK_END); /* Get to EOF */
  276.     cnt = 0;
  277.     printf("Starting the expansion\n");
  278.     while (myread(temp, MSG_SECT_SIZE, 1, Amsgfl) == 1)
  279.       {
  280.       printf("reading %08.8d\r",++cnt);
  281.       };
  282.     printf("\n");
  283.     fudge = copy_remainder(offset);
  284.     for (i = 0; i < MSG_SECT_SIZE; i++) temp[i] = 0;
  285.     crypte(temp, MSG_SECT_SIZE, 0);
  286.     i = cfg.maxMSector - oldsize - fudge;
  287.     printf("And now %7d sectors left to initialize\n", i);
  288.  
  289.     for (; i; i--)
  290.       {
  291.       printf("%7d\r", i);
  292.       if (mywrite(temp, MSG_SECT_SIZE, 1, Amsgfl) != 1)
  293.         {
  294.         printf("****Error writing expansion sectors(%d left to do)\n",i);
  295.         exit(100);
  296.         };
  297.       };
  298.     myclose(Amsgfl);
  299.     printf("\nFinished.  Don't need to reconfigure!  But don't forget");
  300.     printf(" to change CTDLCNFG.SYS.\n ");
  301.  
  302.     }
  303.   return 0;
  304.   }
  305. /*
  306. * InitMsgBase()
  307. *
  308. * This function opens the msg base(s), inits the msg buffers.
  309. */
  310. void Expand_InitMsgBase()
  311.   {
  312.   SYS_FILE name;
  313.   makeSysName(name, "ctdlmsg.sys", &cfg.msgArea);
  314.   SopenFile(name, &Amsgfl);
  315.   InitBuffers();
  316.  
  317.   }
  318. /*
  319. * SopenFile()
  320. *
  321. * This opens one of the .sys files.   Has extra parameter for mode
  322. * which is global.
  323. */
  324. void SopenFile(char *filename, BPTR *fd)
  325.   {
  326.  
  327.   /* We use fopen here rather than safeopen for link reasons */
  328.   if ((*fd = myopen(filename, mode)) == NULL)
  329.     {
  330.     printf("?no %s", filename);
  331.     exit(SYSOP_EXIT);
  332.  
  333.     }
  334.  
  335.   }
  336.  
  337. long myseek( BPTR fd, long rpos, int mode)
  338.   {
  339.  
  340.   switch (mode )
  341.     {
  342.     case SEEK_SET: mode = OFFSET_BEGINNING;break;
  343.     case SEEK_CUR: mode = OFFSET_CURRENT  ;break;
  344.     case SEEK_END: mode = OFFSET_END      ;break;
  345.     default:
  346.     fprintf(stderr,"Error Invalid fseek mode:%d\n",mode);
  347.     return -1;
  348.     };
  349.  
  350.   if( fd == NULL )
  351.     {
  352.     fprintf(stderr, "Error NULL file pointer\n");
  353.     return -1;
  354.     };
  355.   (void)Seek( fd, rpos, mode );
  356.   return 0;
  357.   }
  358.  
  359. BPTR myopen( char *name, char *mode )
  360.   {
  361.   return Open( name, MODE_OLDFILE);
  362.   }
  363.  
  364. void myclose(BPTR fd)
  365.   {
  366.   Close(fd);
  367.   }
  368.  
  369. long  myread(void *buff, int size, int blocks, BPTR fp    )
  370.   {
  371.   long rsize, isize;
  372.   rsize = size * blocks;
  373.   isize =  Read( fp, buff, rsize  );
  374.   return (isize/size);
  375.   }
  376.  
  377.  
  378. long  mywrite(void *buff, int size, int blocks, BPTR fp    )
  379.   {
  380.   long rsize, isize;
  381.   rsize = size * blocks;
  382.   isize =  Write( fp, buff, rsize  );
  383.   return (isize/size);
  384.   }
  385.